import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import java.util.List;

public class aaa {
    public WebDriver driver = null;
    public void start()
    {
//        1、打开浏览器（驱动）
        WebDriverManager.chromedriver().setup();
//        2)添加浏览器配置
        ChromeOptions options = new ChromeOptions();
//        允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        driver = new ChromeDriver(options);

    }

    public void stop()
    {
        driver.quit();
    }

    //元素的查找和定位
    public void test01()
    {
        start();
        driver.get("https://www.baidu.com/");
        //查找一个
//        WebElement ele = driver.findElement(By.cssSelector("#chat-textarea"));
//        System.out.println(ele);


        //查找多个
//        List<WebElement> eles = driver.findElements(By.xpath("//*[@id=\"s-hotsearch-wrapper\"]/ul/li"));
//        List<WebElement> eles = driver.findElements(By.xpath("//*[@id=\"s-hotsearch-wrapper\"]/ul/li/a/span[2]"));
//        for(WebElement ele : eles)
//        {
//            System.out.println(ele.getText());
//        }



        //定位 css
        //新闻
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title"));

        //xpath
        driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]"));
        driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[1]/a/span[2]"));


        //查找不存在的元素
        driver.findElement(By.xpath("//*[@id=\"hotsearch-contentaaaaa-wrapper\"]/li[1]/a/span[2]"));
        stop();
    }
}
